package data.nanjing.expo.ctrls;

import data.nanjing.expo.common.Result;
import data.nanjing.expo.models.domain.AuthInfo;
import data.nanjing.expo.models.domain.User;
import data.nanjing.expo.models.domain.dto.BaseUser;
import data.nanjing.expo.services.AuthInfoService;
import data.nanjing.expo.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * Created by zjp on 2018/7/2.
 * QQ：34948062
 * github: https://www.github.com/zjp1029
 * web： http://www.zjp1029.cn
 */

@RestController
@RequestMapping("/api/user")
public class UserCtrl extends BaseCtrl {

    @Autowired
    private UserService userService;

    @Autowired
    private AuthInfoService authInfoService;


    /**
     * 添加个人认证信息
     * @param authInfo
     * @param request
     * @return
     */
    @PostMapping("/auth/person")
    public Result personAuth(@Valid AuthInfo authInfo, HttpServletRequest request) {
        try {
            User user = this.getUserFromSession(request);
            if (user == null) {
                return send(-2, "请登录");
            }
            AuthInfo info = authInfoService.selectAuthInfoByUserId(user.getId());
            authInfo.setUserId(user.getId());
            if (info == null) {
                authInfoService.addPersonAuthInfo(authInfo);
            }else {
                authInfoService.updatePersonAuthInfo(authInfo);
            }
            return send(1, "提交认证申请成功");
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "认证身份失败");
        }
    }


    /**
     * 保存企业认证信息
     * @param authInfo
     * @param request
     * @return
     */
    @PostMapping("/auth/company")
    public Result companyAuth(@Valid AuthInfo authInfo, HttpServletRequest request) {
        try {
            User user = this.getUserFromSession(request);
            if (user == null) {
                return send(-2, "请登录");
            }
            AuthInfo info = authInfoService.selectAuthInfoByUserId(user.getId());
            authInfo.setUserId(user.getId());
            if (info == null) {
                authInfoService.addCompanyAuthInfo(authInfo);
            }else {
                authInfoService.updateCompanyAuthInfo(authInfo);
            }
            return send(1, "申请验证成功");
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "认证身份失败");
        }
    }


    /**
     * 获取认证结果
     * 逻辑: 前端发送认证情况获取，如果是企业认证，则获取企业认证的认证情况
     * 如果是个人认证，则先判断有没有进行企业认证；如果企业认证通过，则不必再个人验证
     * 如果企业认证未通过或待审核，则可以修改认证，改为个人认证
     *
     * @param type company:企业认证; person:个人认证
     * @param request
     * @return
     */
    @GetMapping("/auth/status/{type}")
    public Result getAuthStatus(@PathVariable("type") String type, HttpServletRequest request) {
        try {
            User user = this.getUserFromSession(request);
            if (user == null) {
                return send(-2, "请登录");
            }
            AuthInfo authInfo = authInfoService.selectAuthInfoByUserId(user.getId());
            if (authInfo == null) {
                return send(1021, "未认证");
            }
            if (type.equals("company")) {
                if ("unaudited".equals(authInfo.getCompanyCheckStatus())) {
                    return send(1022, "待审核");
                }else if ("refuse".equals(authInfo.getCompanyCheckStatus() )) {
                    return send(authInfo, 1023,"认证未通过");
                }else if ("pass".equals(authInfo.getCompanyCheckStatus())) {
                    return send(authInfo, 1020, "认证通过");
                }else {
                    return send(1021, "未认证");
                }
            }else {
                if ("unaudited".equals(authInfo.getPersonCheckStatus())) {
                    return send(1022, "待审核");
                }else if ("refuse".equals(authInfo.getPersonCheckStatus() )) {
                    return send(authInfo, 1023,"认证未通过");
                }else if ("pass".equals(authInfo.getPersonCheckStatus())) {
                    return send(authInfo, 1020, "认证通过");
                }else {
                    return send(1021, "未认证");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "获取数据失败");
        }
    }

    /**
     * 更新信息
     * type: company：企业;person:个人
     * @param type
     * @param authInfo
     * @param request
     * @return
     */
    @PostMapping("/auth/update/{type}")
    public Result updateAuthInfo(@PathVariable("type") String type, @Valid AuthInfo authInfo, HttpServletRequest request) {
        try {
            User user = this.getUserFromSession(request);
            if (user == null) {
                return send(-2, "请登录");
            }
            authInfo.setUserId(user.getId());
            if ("company".equals(type)) {
                authInfoService.updateCompanyAuthInfo(authInfo);
            }else {
                authInfoService.updatePersonAuthInfo(authInfo);
            }
            return send(1, "提交申请成功");
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "修改认证资料失败");
        }
    }

    /**
     * 更细个人基本信息
     * @param user
     * @return
     */
    @PostMapping("/base/update")
    public Result updateBaseInfo(@Valid BaseUser user, HttpServletRequest request) {
        try {
            User loginUser = this.getUserFromSession(request);
            if (loginUser == null) {
                return send(-2, "请登录");
            }
            user.setId(loginUser.getId());
            user.setPhoneNo(loginUser.getPhoneNo());
            userService.updatePeopleBaseInfo(user);
            return send(1, "修改成功");
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "更新错误");
        }
    }

    /**
     * 获取个人基本信息
     * @return
     */
    @GetMapping("/base/info")
    public Result getUserBaseInfo(HttpServletRequest request) {
        try {
            User user = this.getUserFromSession(request);
            if (user == null) {
                return send(-2, "请登录");
            }
            User u = userService.selectUserInfoByPhoneNo("13270764700");
            return send(u, 1, "获取成功");
        }catch (Exception e) {
            e.printStackTrace();
            return send(-1, "出错了");
        }
    }


}
